package cn.jdy.plugins.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.authority.entity.User;
import cn.jdy.authority.entity.UserRole;
import cn.jdy.authority.service.OrganService;
import cn.jdy.authority.service.UserRoleService;
import cn.jdy.framework.exception.BaseException;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.framework.util.StringUtil;
import cn.jdy.platform.service.ConfService;
import cn.jdy.plugins.entity.Label;
import cn.jdy.plugins.service.LabelService;
import cn.jdy.plugins.service.LiveService;

/**
 * 系统页签管理控制类
 * 
 * @author jga
 * @description: 针对页签统一管理的控制类。
 * @date 2017-07-28 10:10
 */
@Controller
@RequestMapping("/label")
public class LabelCtrl {
	@Autowired
	protected HttpServletRequest request;
	@Autowired
	protected HttpSession session;
	// 自动注入页签管理业务处理操作接口
	@Autowired
	private LabelService LabelService;
	@Autowired
	private LiveService liveService;
	@Autowired
	private OrganService organ;
	@Autowired
	private ConfService confService;
	@Autowired
	private UserRoleService userRoleService;
	/**
	 * 初始化
	 * 
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/listin")
	@ResponseBody
	public ModelAndView list(Label entity, String fromDate, String toDate,String liveid) {
		User user = (User) session.getAttribute("user");
		ModelAndView mv = new ModelAndView("/operate/labelList.jsp");
		mv.addObject("totals", LabelService.count("liveid",entity.getLiveid()));
		mv.addObject("liveid",liveid);
		return mv;
	}

	/**
	 * 页签列表 异步数据查询
	 * 
	 * @return Map<Label> 页签(Label)列表
	 * @description: 根据页面不同查询条件 获取系统用户列表信息
	 * @param: map 页签查询条件
	 * @param: curNo 列表查询当前页码
	 * @param: curSize 最大记录位置
	 * @author: jga
	 * @date: 2017-03-23 10:20
	 */
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(Label sp) {

		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			User user = (User) session.getAttribute("user");
			List<UserRole> list = userRoleService.find("userid", user.getId());
			String roleid = "";
			for (int i = 0; i < list.size(); i++) {
				if (list.get(i).getRoleid()
						.equals("4028b3044de1a396014de1ba2b2e0000")
						|| list.get(i).getRoleid()
								.equals("ff8080814db89f0a014db8d169290000")) {
					roleid = "0";
				}
			}
			if (roleid != "0") {
				sp.setUserid(user.getId());
			}else{
			}
			jsonMap.put("Rows", LabelService.query(sp));
			jsonMap.put("Total", LabelService.count(sp));
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}

	/**
	 * 页签信息 新增或修改
	 * 
	 * @return String status
	 * @author: lutan
	 */
	@RequestMapping(value = "/addlabel", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addLabel(Label sp) {
		ModelAndView mv = new ModelAndView();
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(StringUtil.isEmpty(sp.getId())) {
				//sp.setLiveid(liveService.findOne("userid",sp.getUserid()).getId());
				sp.setCreateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.LabelService.save(sp);
			} else {// 更新
				sp.setLiveid(LabelService.findOne("id",sp.getId()).getLiveid());
				sp.setUserid(LabelService.findOne("id",sp.getId()).getUserid());
				sp.setUpdateTime(DateUtil.getCurrentDate(DateUtil._DEFAULT2));
				this.LabelService.update(sp);
			}
			jsonMap.put("statusCode", 200);
		} catch (Exception e) {
			mv.addObject("statusCode", "500");
			// mv.setViewName(this.viewName.concat("4"));
			BaseLog.e(getClass(), "信息保存异常！", e);
		}

		return jsonMap;
	}

	/**
	 * 修改初始化
	 * 
	 * @description: 修改初始化
	 */
	@RequestMapping("/updlabelInit")
	@ResponseBody
	public Label updLabelInit(String id) {
		Label label = null;
		try {
			// 页签修改初始化
			label = LabelService.findOne("id", id);
		} catch (Exception e) {
			BaseLog.e(this.getClass(), "updUserInit 修改前查询", e);
		}
		return label;
	}

	/**
	 * 删除
	 * 
	 * @return Map 删除页签的成功、失败信息
	 * @description: 删除指定用户标识的系统页签, 对多个系统页签的删除操作
	 * @author: jga
	 * @date: 2017-03-23 16:40
	 */
	@RequestMapping("/dellabel")
	@ResponseBody
	public Map<String, Object> delUser(String userIds) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		String backInfo = "";
		if ("".equals(backInfo)) {
			backInfo = LabelService.delUserById(userIds);
			jsonMap.put("message", backInfo);
		}
		return jsonMap;
	}

	/**
	 * 查询根据机构id查询，机构下面的页签
	 * 
	 * type 等0 代表是主页签，1代表是备用页签
	 * 
	 * @author: jga
	 * @date: 2017-03-23 16:40
	 */
	@RequestMapping("/selectzlabel")
	public ModelAndView selectzLabel(Label Label, int type) {
		ModelAndView mv = new ModelAndView();
		String backInfo = "";

		if ("".equals(backInfo)) {

			DetachedCriteria dc = DetachedCriteria.forClass(Label.class);
			dc.addOrder(Order.desc("createTime"));
			List<Label> list = LabelService.find(dc);
			mv.addObject("datelist", list);

		}
		if (type == 0) {// 0代表进去主页签ajax页面
			mv.setViewName("/platform/ajaxzLabel.jsp");
		} else {
			mv.setViewName("/platform/ajaxbLabel.jsp");
		}

		return mv;
	}

	/**
	 * 获取组织机构树
	 * 
	 * @return map
	 * @description: 获取组织机构树
	 * @param: organNo 机构编码
	 * @author: wuzhiliang
	 * @date: 2013-08-05 16:53
	 */
	@RequestMapping("/organTree")
	@ResponseBody
	public Map<String, Object> organTree(String istype) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("orgList", confService.queryByType(51, null, null));
			jsonMap.put("msg", "success");
		} catch (BaseException e) {
			jsonMap.put("msg", "error");
			BaseLog.e(this.getClass(), "获取组织机构树出错! findOrgCount! ", e);
		}
		return jsonMap;
	}

}
